#!/usr/bin/env python3
import sys
import argparse
import shutil
import os
from pathlib import Path
import subprocess

outputTypeMap = {
        'release': 'Release',
        'test'   : 'Debug'
        }

cmakeDefineMap = {
    'release': '',
    'test'   : '-DO5G_TEST=1'
}

def createOutputDir(dirName:str) -> None:
    outputDir = Path(f'build-{dirName}')
    if not outputDir.is_dir():
        print(f'create output dir build-{dirName}')
        os.makedirs(f'./build-{dirName}')

def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument('-t',
                        '--type',
                        action = 'append',
                        choices = ['release', 'test'],
                        help = 'specify build type')
    parser.add_argument('-c',
                        '--clean',
                        action = 'store_true',
                        default = False,
                        help = 'erase output dirs')

    args = parser.parse_args()

    if not args.type:
        if args.clean:
            print('removing output dir')
            return subprocess.call(f'rm -rf build-*', shell=True)
    else:
        for type_ in args.type:
            createOutputDir(type_)
            cmd = f'''cd build-{type_}; cmake -DCMAKE_BUILD_TYPE={outputTypeMap[type_]} {cmakeDefineMap[type_]} -G "CodeBlocks - Unix Makefiles" .. && make -j 8;'''
            print(f'execute command: {cmd}')
            return subprocess.call(cmd, shell=True)

if __name__ == '__main__':
    main()
    print('done')
